<?php
header("Content-type:text/html; charset=utf-8");
require_once ('include/conn.php');
require_once ('system.class.php');
define('XunSearchPath', "/home/acer/xunsearch");
require_once (XunSearchPath.'/sdk/php/lib/XS.php');
function searchlist($args, $content, &$smarty){

	$key_words=$args['key_words'];
	$limit=20;
	if(isset($args['limit']))
		$limit=$args['limit'];
	if($limit<0){
		$limit=20;
	}
	$offset=0;
	if(isset($args['offset'])){
		$offset=$args['offset'];
	}
	if($offset<0){
		$offset=0;
	}
	/*info数组，存储数量等信息,arr数组，存储结果信息*/
	$info=array();
	$arr=array();
	/*初始化*/
	$xs=new XS('nwsuafcie');
	$search=$xs->search;


	/*获取热搜词*/
	$hot_words = $search->getHotQuery(10,'currnum');
	$info['hot_words']=array_keys($hot_words);
	/*获取数目信息*/
	$count=$search->count($key_words);
	if($offset>=$count){
		$offset=($count%$limit==0)?($count-$limit):($count-($count%$limit));
	}
	if($offset%$limit!=0){
		$offset=$offset-$offset%$limit;
	}


	/*获取搜索结果信息*/
	$search->setQuery($key_words);
	$search->setSort('article_modify_time');
	$docs = $search->setLimit($limit,$offset)->search($key_words);
	/*获取可能存在的正确搜索提示词*/
	$corrected = $search->getCorrectedQuery();
	if(count($corrected)!=0){
		$info['corrected']=$corrected;
	}else{
		$info['corrected']="";
	}
	/*获取相关搜索词*/
	$related_words = $search->getRelatedQuery();
	$info['related_words']=$related_words;
	/*获取查询结果*/
	foreach ($docs as $doc)
	{
		$article_id=$doc->id;
		$article_info=getArticleInfor($article_id);
		if($article_info==""){
			continue;
		}
		$article_info= explode("|", $article_info);
		$arr[$article_id]['rank']=$doc->rank();
		$arr[$article_id]['percent']=$doc->percent();
		$arr[$article_id]['id']=$article_id;
		$arr[$article_id]['title']=$search->highlight($doc->article_title);
		$arr[$article_id]['article_from']=$doc->article_from;
		$arr[$article_id]['publish_time']=date("Y-m-d H:i:s", $doc->article_publish_time);
		if(@$article_info[2]<1){
				$arr[$article_id]['url']="article.php?article_id=$doc->id&channel_id=$doc->article_main_channel";
			    $arr[$article_id]['content']=$search->highlight($doc->article_content);
		}else{
			$arr[$article_id]['url']=$article_info[3];
			$arr[$article_id]['content']="该文章为外链文章";
		}
	}

	/*文章数*/
	$info['article_num']=$count;
	/*首页*/
		$link=CIE."/search.php?offset=0&keyword=$key_words";
	$info['article_home']="<span class='page'><a href='$link'>首页</a></span>";
	/*上一页*/
	$info['pre_page']="";
	if ($offset>0) {
		$prevoffset=$offset-$limit;
		if($prevoffset>=0){
				$link=CIE."/search.php?offset=$prevoffset&keyword=$key_words";
		}
		$info['pre_page']="<span class='page'><a href='$link'>上一页</a></span>";
	}
	/*计算页面数*/
	$pages=intval($count/$limit);

	/*如果有余数，加一*/
	if ($count%$limit) {
		$pages++;
	}
	/*显示页数*/
	$info['pages']=null;
	$current_num=$offset/$limit+1;
	$i=($current_num>3)?($current_num-2):1;
	$limit_num=$i+5;
	while($i<=$pages&&$i<$limit_num) {
		$newoffset=$limit*($i-1);
		$page_num=$i;
		if($i==$current_num){
			$page_num="<span class='current_page'>$i</span>";
		}
			$link=CIE."/search.php?offset=$newoffset&keyword=$key_words";
		$info['pages'][$i]="<span class='page'><a href='$link'>$page_num</a></span>";
		$i++;
	}

	/*下一页*/
	$info['next_page']="";
	if (!(($offset/$limit)==$pages) && $pages!=1) {
		$newoffset=$offset+$limit;
		if($newoffset<$count){
			$link=CIE."/search.php?offset=$newoffset&keyword=$key_words";
			$info['next_page']="<span class='page'><a href='$link'>下一页</a></span>";
		}
	}
	/*尾页*/
	$newoffset=($count%$limit)?($count-$count%$limit):($count-$count%$limit-$limit);
	if($newoffset<0){
		$newoffset=0;
	}
	$link=CIE."/search.php?offset=$newoffset&keyword=$key_words";
	$info['last_page']="<span class='page'><a href='$link'>尾页</a></span>";
	if(empty($info)){
		$infor=null;
	}
	if(empty($arr)){
		$arr=null;
	}
	$smarty->assign("search",$arr);
	$smarty->assign("info",$info);
	return $content;
}
function getArticleInfor($article_id){
	$sql_get_article_information="select * from article_normal where article_id=$article_id;";
	$result_get_article_information=mysql_query($sql_get_article_information);
	$row_get_article_information=mysql_fetch_row($result_get_article_information);
	if(!empty($row_get_article_information)){
		return join("|",$row_get_article_information);
	}
	return "";
}
